package com.ihome.excel.xlsx.engine.export;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.poi.ss.usermodel.Cell;

import com.ihome.excel.xlsx.config.DataMappingConfig;
import com.ihome.excel.xlsx.entity.DataType;
import com.ihome.excel.xlsx.exceptions.ThrowableDefine;

public class CellWriteEngine {

	public void writeCellValue(Cell cell,Object value) {
		if(cell == null) {
			throw ThrowableDefine.PARAMETER_VALIDATE_EXP.getIllegaArgumentExp("cell");
		}
		if(value == null) {
			cell.setCellValue("");
			return;
		}
		String valueType = value.getClass().getName();
		String mappingType = DataMappingConfig.getPropertyValue(valueType);
		int dataType = DataType.getDataType(mappingType).getCode();
		switch (dataType) {
			case 1:
				cell.setCellValue(String.valueOf(value));
				break;
			case 2:
				cell.setCellValue(Double.parseDouble(value.toString()));
				break;
			case 3:
				cell.setCellValue(String.valueOf(value));
				break;
			case 4:
				cell.setCellValue(String.valueOf(value));
				break;
			case 5:
				cell.setCellValue((Date) value);
				break;
			case 6:
				cell.setCellValue(((BigDecimal) value).doubleValue());
				break;
			default:
				cell.setCellValue(String.valueOf(value));
				break;
		}
	}
	
}
